/*jslint sub: true */
/*global Class, Jedi */

/**
 * @class
 * @name Jedi.Panel
 * @extends Class
 */
Jedi.Panel = Class.$extend({

    /**
     * @private
     * @field
     * @name Jedi.Panel#name
     * @see Jedi.Panel#getName
     * @type {String|null}
     */
    name : null,

    /**
     * @private
     * @field
     * @name Jedi.Panel#table
     * @see Jedi.Panel#getTable
     * @type {String|null}
     */
    table : null,

    /**
     * @private
     * @field
     * @name Jedi.Panel#view
     * @see Jedi.Panel#getView
     * @type {String|null}
     */
    view : null,

    /**
     * @private
     * @field
     * @name Jedi.Panel#useUpdate
     * @see Jedi.Panel#isUseUpdate
     * @type {Boolean|null}
     */
    useUpdate : null,

    /**
     * @private
     * @field
     * @name Jedi.Panel#useInsert
     * @see Jedi.Panel#isUseInsert
     * @type {Boolean|null}
     */
    useInsert : null,

    /**
     * @private
     * @field
     * @name Jedi.Panel#useSelect
     * @see Jedi.Panel#isUseSelect
     * @type {Boolean|null}
     */
    useSelect : null,

    /**
     * @private
     * @field
     * @name Jedi.Panel#useDelete
     * @see Jedi.Panel#isUseDelete
     * @type {Boolean|null}
     */
    useDelete : null,

    /**
     * @private
     * @field
     * @name Jedi.Panel#useAcl
     * @see Jedi.Panel#isUseAcl
     * @type {Boolean|null}
     */
    useAcl : null,

    /**
     * @private
     * @field
     * @name Jedi.Panel#primary
     * @see Jedi.Panel#getPrimary
     * @type {String[]|null}
     */
    primary : null,

    /**
     * @private
     * @field
     * @name Jedi.Panel#columns
     * @see Jedi.Panel#getColumns
     * @type {Jedi.PanelsList|null}
     */
    columns : null,

    /**
     * Конструктор.
     *
     * @public
     * @function
     * @name Jedi.Panel#$init
     * @param {String} name
     * @param {Object} config
     * @return {void}
     */
    $init : function (name, config) {
        'use strict';
        config = config || {};
        this.name = name;
        if (typeof name !== 'string' || !/^[a-z]\w*$/.test(name)) {
            this.name = null;
            throw new TypeError('');
        }
        this.table = name;
        if (typeof config['table'] !== 'undefined') {
            if (typeof config['table'] !== 'string' || !/^[a-z]\w*$/.test(config['table'])) {
                throw new TypeError('');
            }
            this.table = config['table'];
        }
        this.view = this.table;
        if (typeof config['view'] !== 'undefined') {
            if (typeof config['view'] !== 'string' || !/^[a-z]\w*$/.test(config['view'])) {
                throw new TypeError('');
            }
            this.table = config['view'];
        }
        this.useUpdate = true;
        if (typeof config['useUpdate'] !== 'undefined') {
            this.useUpdate = !!config['useUpdate'];
            if (typeof config['useUpdate'] === 'string') {
                this.useUpdate = ['true', 'yes', 'on', '1'].
                    indexOf(config['useUpdate'].toLowerCase()) !== -1;
            } else if (typeof config['useUpdate'] === 'number') {
                this.useUpdate = config['useUpdate'] > 0;
            } else if (typeof config['useUpdate'] === 'boolean') {
                this.useUpdate = !!config['useUpdate'];
            }
        }
        this.useInsert = true;
        if (typeof config['useInsert'] !== 'undefined') {
            this.useInsert = !!config['useInsert'];
            if (typeof config['useInsert'] === 'string') {
                this.useInsert = ['true', 'yes', 'on', '1'].
                    indexOf(config['useInsert'].toLowerCase()) !== -1;
            } else if (typeof config['useInsert'] === 'number') {
                this.useInsert = config['useInsert'] > 0;
            } else if (typeof config['useInsert'] === 'boolean') {
                this.useInsert = !!config['useInsert'];
            }
        }
        this.useSelect = true;
        if (typeof config['useSelect'] !== 'undefined') {
            this.useSelect = !!config['useSelect'];
            if (typeof config['useSelect'] === 'string') {
                this.useSelect = ['true', 'yes', 'on', '1'].
                    indexOf(config['useSelect'].toLowerCase()) !== -1;
            } else if (typeof config['useSelect'] === 'number') {
                this.useSelect = config['useSelect'] > 0;
            } else if (typeof config['useSelect'] === 'boolean') {
                this.useSelect = !!config['useSelect'];
            }
        }
        this.useDelete = true;
        if (typeof config['useDelete'] !== 'undefined') {
            this.useDelete = !!config['useDelete'];
            if (typeof config['useDelete'] === 'string') {
                this.useDelete = ['true', 'yes', 'on', '1'].
                    indexOf(config['useDelete'].toLowerCase()) !== -1;
            } else if (typeof config['useDelete'] === 'number') {
                this.useDelete = config['useDelete'] > 0;
            } else if (typeof config['useDelete'] === 'boolean') {
                this.useDelete = !!config['useDelete'];
            }
        }
        this.useAcl = true;
        if (typeof config['useAcl'] !== 'undefined') {
            this.useAcl = !!config['useAcl'];
            if (typeof config['useAcl'] === 'string') {
                this.useAcl = ['true', 'yes', 'on', '1'].
                    indexOf(config['useAcl'].toLowerCase()) !== -1;
            } else if (typeof config['useAcl'] === 'number') {
                this.useAcl = config['useAcl'] > 0;
            } else if (typeof config['useAcl'] === 'boolean') {
                this.useAcl = !!config['useAcl'];
            }
        }
        this.columns = Jedi.ColumnsList.$create(config['columns']);
    },

    /**
     * @public
     * @function
     * @name Jedi.Panel#getName
     * @see Jedi.Panel#name
     * @return {String}
     */
    getName : function () {
        'use strict';
        return this.name;
    },

    /**
     * @public
     * @function
     * @name Jedi.Panel#getTable
     * @see Jedi.Panel#table
     * @return {String}
     */
    getTable : function () {
        'use strict';
        return this.table;
    },

    /**
     * @public
     * @function
     * @name Jedi.Panel#getView
     * @see Jedi.Panel#view
     * @return {String}
     */
    getView : function () {
        'use strict';
        return this.view;
    },

    /**
     * @public
     * @function
     * @name Jedi.Panel#getPrimary
     * @see Jedi.Panel#primary
     * @return {String[]}
     */
    getPrimary : function () {
        'use strict';
        return this.primary;
    },

    /**
     * @public
     * @function
     * @name Jedi.Panel#isUseUpdate
     * @see Jedi.Panel#useUpdate
     * @return {Boolean}
     */
    isUseUpdate : function () {
        'use strict';
        return !!this.useUpdate;
    },

    /**
     * @public
     * @function
     * @name Jedi.Panel#isUseInsert
     * @see Jedi.Panel#useInsert
     * @return {Boolean}
     */
    isUseInsert : function () {
        'use strict';
        return !!this.useInsert;
    },

    /**
     * @public
     * @function
     * @name Jedi.Panel#isUseSelect
     * @see Jedi.Panel#useSelect
     * @return {Boolean}
     */
    isUseSelect : function () {
        'use strict';
        return !!this.useSelect;
    },

    /**
     * @public
     * @function
     * @name Jedi.Panel#isUseDelete
     * @see Jedi.Panel#useDelete
     * @return {Boolean}
     */
    isUseDelete : function () {
        'use strict';
        return !!this.useDelete;
    },

    /**
     * @public
     * @function
     * @name Jedi.Panel#isUseAcl
     * @see Jedi.Panel#useAcl
     * @return {Boolean}
     */
    isUseAcl : function () {
        'use strict';
        return !!this.useAcl;
    },

    /**
     * @public
     * @function
     * @name Jedi.Panel#getColumns
     * @see Jedi.Panel#columns
     * @return {Jedi.PanelsList}
     */
    getColumns : function () {
        'use strict';
        return this.columns;
    }

});
